<template>

<el-main> 
  <!-- <el-input v-model="form.userName" placeholder="输入用户名" style="width:150px">
  </el-input>
  <el-button type="primary" @click="onSubmit">查询</el-button> -->
 <!-- <el-autocomplete style="width:100%" class="inline-input" v-model="inputName" :fetch-suggestions="querySearch" placeholder="请输入文件名"></el-autocomplete> -->

   <!-- <el-input v-model="search" placeholder="请输入内容" style="width:240px"></el-input> -->
  <el-input v-model="userName" placeholder="请输入用户名" style="width:240px"></el-input>
  <el-button type="primary" @click="requestData()">搜索</el-button>

<!--  这是显示表 --><br/><br/>
 <el-button plain size="mini" type="primary" @click="addDialog()">添加</el-button>
 <!-- <el-button plain size="mini" type="primary" @click="dialogVisible2 = true">修改</el-button> -->
 <!-- <el-button plain size="mini" type="primary" @click="dialogVisible = true">修改</el-button> -->
 <el-button plain size="mini" type="primary" @click="handleDelete(scope.row)">批量删除</el-button>
  
 <el-table ref="multipleTable" :data="tableData3" tooltip-effect="dark" style="width: 100%" @selection-change="handleSelectionChange">
    <el-table-column type="selection" width="55">
    </el-table-column>
    <el-table-column fixed prop="id" label="编号" width="150">
    </el-table-column>
   <el-table-column prop="userName" label="用户名" width="120">
    </el-table-column>
    <el-table-column prop="password" label="密码" width="120">
    </el-table-column>
    <el-table-column prop="trueName" label="真实姓名" width="120">
    </el-table-column>
    <el-table-column prop="remarks" label="备注" width="300">
    </el-table-column>
    <el-table-column prop="roles" label="拥有角色" width="120">
    </el-table-column>
     <el-table-column label="操作">
      <template slot-scope="scope">
        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
        <!-- <el-button plain size="mini" type="primary" @click="update(index,row)">修改</el-button> -->
         <el-button plain size="mini" type="primary" @click="modify(scope.row)">修改</el-button>
       
      </template>
      </el-table-column>
  </el-table>
   <el-pagination
      @size-change="handleSizeChange" 
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="pageSizes"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>

<!-- 这是他们弹出的添加表单  -->
 <!-- <el-button type="text" @click="dialogVisible = true">添加</el-button> -->
     <el-dialog title="添加页面" :visible.sync="dialogVisible" width="30%" @close="reform(form)">     
  
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="用户名" prop="userName" :rules="[
      { required: true, message: '请输入用户名', trigger: 'blur' }]">
          <el-input v-model="form.userName"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password" :rules="[
      { required: true, message: '请输入密码', trigger: 'blur' }]">
          <!-- <el-input type="text" style="display: none"> -->
          <el-input type="password" v-model="form.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="真实姓名" prop="trueName" :rules="[
      { required: true, message: '请输入真实姓名', trigger: 'blur' }]
   ">
          <el-input v-model="form.trueName"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remarks" :rules="[
      { required: true, message: '请输入备注', trigger: 'blur' }]
   ">
          <el-input v-model="form.remarks"></el-input>
        </el-form-item>
        <el-form-item label="拥有角色" prop="roles" :rules="[
      { required: true, message: '请输入备注', trigger: 'blur' }]
   ">
       <el-select v-model="form.roles" placeholder="请选择拥有角色">
          <el-option label="管理员" value="shanghai"></el-option>
          <el-option label="销售经理" value="beijing"></el-option>
       </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addUserOff('form')">取 消</el-button>
         <!-- <el-button @click="dialogVisible = false">取 消</el-button> -->
        <el-button type="primary" @click="addData()">确 定</el-button>
      </span>
    </el-dialog>
<!-- 这是他们弹出的添加表单以此为截至  -->

    <!--修改弹框-->
    <el-dialog title="修改页面" :visible.sync="dialogVisible2" width="30%">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="用户名" prop="userName" :rules="[
      { required: true, message: '请输入用户名', trigger: 'blur' }]">
          <el-input v-model="form.userName"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password" :rules="[
      { required: true, message: '请输入密码', trigger: 'blur' },
    ]
   ">
          <el-input v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item label="真实姓名" prop="trueName" :rules="[
      { required: true, message: '请输入真实姓名', trigger: 'blur' }]
   ">
          <el-input v-model="form.trueName"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remarks" :rules="[
      { required: true, message: '请输入备注', trigger: 'blur' }]
   ">
          <el-input v-model="form.remarks"></el-input>
        </el-form-item>
        <el-form-item label="拥有角色" prop="roles" :rules="[
      { required: true, message: '请输入备注', trigger: 'blur' }]
   ">
       <el-select v-model="form.roles" placeholder="请选择拥有角色">
          <el-option label="" value="shanghai"></el-option>
          <el-option label="" value="beijing"></el-option>
       </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="update(row)">确 定</el-button>
      </span>
    </el-dialog>
   <!-- 修改页面以此为截至  -->
     


  
  </el-main>
</template>

<script>

export default {
    data() {
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.form.password !== '') {
            this.$refs.form.validateField('password');
          }
          callback();
        }
      };
        return {
             tableData3:[
              {
             id:'',
             userName:'',
             password:'',
             trueName:'',
             remarks:'',
             roles:''
              }
             ],
             dialogVisible:false,
             dialogVisible2:false,
             form: {
             id:null,
             userName:'',
             password:'',
             trueName:'',
             remarks:'',
             roles:''
             },
             userName:'',
            currentPage:1,
             pageSizes:[1, 2, 3, 4],
             pageSize:1,
             total:5,
             page:{}   
        }
    },
    mounted(){
       this.requestData();

    },
    methods:{
       handleSizeChange(val) {
        console.log(`每页 ${val} 条`);
        this.pageSize = val;
         this.requestData();
      },
      handleCurrentChange(val) {
        console.log(`当前页: ${val}`);
        this.currentPage = val;
        this.requestData();
      },
     
        addDialog(){
            this.dialogVisible = true;
            for(let key in this.form){
                this.form[key]  = '';
            }
        },
         addData(){  
            var userName=this.form.userName;
            var password=this.form.password;
            var trueName=this.form.trueName;
            var remarks=this.form.remarks;
            var roles=this.form.roles;
            var formDate =new window.FormData();
            formDate.append('userName',userName);
            formDate.append('password',password);
            formDate.append('trueName',trueName);
            formDate.append('remarks',remarks);
            formDate.append('roles',roles);
            var api='http://localhost:8089/admin/user/vue/main/add';
            this.$http.post(api,formDate).then(function(date){
            if(date.body.code=='10000'){
                this.requestData();
            }
             alert(date.body.msg);
             this.dialogVisible = false;

         })
           
      },
      reform(form) {
        this.dialogVisible = false;
        this.$refs[form].resetFields();
      },
  modify(row) { 
     console.log(row);
     for(let key in this.form){
            this.form[key]  = '';
         }
      this.dialogVisible2 = true;
      this.form.userName = row.userName;
      this.form.password = row.password;
      this.form.trueName = row.trueName;
      this.form.remarks = row.remarks;
      this.form.roles = row.roles;
      this.form.id = row.id;
  },
  update(row){  
            console.log(this.form.id);
            var id=this.form.id;
            var userName=this.form.userName;
            var password=this.form.password;
            var trueName=this.form.trueName;
            var remarks=this.form.remarks;
            var roles=this.form.roles;
            var formDate =new window.FormData();
            formDate.append('userName',userName);
            formDate.append('password',password);
            formDate.append('trueName',trueName);
            formDate.append('remarks',remarks);
            formDate.append('roles',roles);
            formDate.append('id',id);
            var api='http://localhost:8089/admin/user/vue/main/update';
            this.$http.post(api,formDate).then(function(date){
            if(date.body.code=='10000'){
                this.requestData();
            }
             alert(date.body.msg);
             this.dialogVisible = false;

         })
           
      },
        requestData(){

          var data = new FormData();
          data.append('userName',this.userName);
          data.append('pageNum',this.currentPage);
          data.append('pageSize',this.pageSize);
           var api = "http://localhost:8089/admin/user/vue/main";
           this.$http.post(api,data).then(response =>{
                 console.log(response);
                 this.tableData3 = response.body.tableData3;
                 this.total =  response.body.total;
                
            })
         },
        handleDelete(row){
           
             console.log(row);
             var formData = new FormData();
             formData.append('id',row.id);
             var api="http://localhost:8089/admin/user/vue/main/del";
             this.$http.post(api,formData).then(function(data){
               if(data.body.code=='10000'){
                   this.requestData();
               }
             alert(data.body.msg);
        
       })
      
      }
    }
    
    
}
</script>

